package com.proactive.webapp.controller;

import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.proactive.webapp.dao.CourseFavoriteDAO;
import com.proactive.webapp.dao.DesignFavoriteDAO;
import com.proactive.webapp.dao.WorksheetFavoriteDAO;
import com.proactive.webapp.entity.CourseFavorite;
import com.proactive.webapp.entity.DesignFavorite;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.WorksheetFavorite;

@Controller
public class FavoriteController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");
	
	@RequestMapping(value = "/addFavorite", method = RequestMethod.POST)
	public @ResponseBody void addFavorite(int type, int id, HttpSession session){
		Member loginMember = (Member)session.getAttribute("loginMember");
		if(loginMember != null){
			String loginAccount = loginMember.getAccount();
			if(type == 1){
				CourseFavoriteDAO courseFavoriteDAO = (CourseFavoriteDAO) context.getBean("courseFavoriteDAO");
				CourseFavorite courseFavorite = new CourseFavorite();
				courseFavorite.setAccount(loginAccount);
				courseFavorite.setCid(id);
				courseFavoriteDAO.insert(courseFavorite);
			}
			else if(type == 2){
				DesignFavoriteDAO designFavoriteDAO = (DesignFavoriteDAO) context.getBean("designFavoriteDAO");
				DesignFavorite designFavorite = new DesignFavorite();
				designFavorite.setAccount(loginAccount);
				designFavorite.setDid(id);
				designFavoriteDAO.insert(designFavorite);
			}
			else if(type == 3){
				WorksheetFavoriteDAO worksheetFavoriteDAO = (WorksheetFavoriteDAO) context.getBean("worksheetFavoriteDAO");
				WorksheetFavorite worksheetFavorite = new WorksheetFavorite();
				worksheetFavorite.setAccount(loginAccount);
				worksheetFavorite.setWid(id);
				worksheetFavoriteDAO.insert(worksheetFavorite);
			}
		}
	}

	@RequestMapping(value = "/delFavorite", method = RequestMethod.POST)
	public @ResponseBody void delFavorite(int type, int id, HttpSession session){
		Member loginMember = (Member)session.getAttribute("loginMember");
		if(loginMember != null){
			String loginAccount = loginMember.getAccount();
			if(type == 1){
				CourseFavoriteDAO courseFavoriteDAO = (CourseFavoriteDAO) context.getBean("courseFavoriteDAO");
				CourseFavorite courseFavorite = new CourseFavorite();
				courseFavorite.setAccount(loginAccount);
				courseFavorite.setCid(id);
				courseFavoriteDAO.delete(courseFavorite);
			}
			else if(type == 2){
				DesignFavoriteDAO designFavoriteDAO = (DesignFavoriteDAO) context.getBean("designFavoriteDAO");
				DesignFavorite designFavorite = new DesignFavorite();
				designFavorite.setAccount(loginAccount);
				designFavorite.setDid(id);
				designFavoriteDAO.delete(designFavorite);
			}
			else if(type == 3){
				WorksheetFavoriteDAO worksheetFavoriteDAO = (WorksheetFavoriteDAO) context.getBean("worksheetFavoriteDAO");
				WorksheetFavorite worksheetFavorite = new WorksheetFavorite();
				worksheetFavorite.setAccount(loginAccount);
				worksheetFavorite.setWid(id);
				worksheetFavoriteDAO.delete(worksheetFavorite);
			}
		}
	}
	
}
